﻿using MyPaint.Models;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MyPaint
{
    public partial class FrmImageProperty : Form
    {
        private SizeF _defaultSize = new SizeF(1152, 648);

        /// <summary>
        /// 默认画布尺寸
        /// </summary>
        public new SizeF DefaultSize
        {
            get { return _defaultSize; }
            set { _defaultSize = value; }
        }

        private SizeF _currentSize = new SizeF(0, 0);

        /// <summary>
        /// 当前画布尺寸
        /// </summary>
        public SizeF CurrentSize
        {
            get { return _currentSize; }
            set { _currentSize = value; }
        }


        private PPFileInfo myFileInfo = new PPFileInfo();

        public FrmImageProperty()
        {
            InitializeComponent();
            SelPixel.Checked = true;
            SelColorful.Checked = true;

            btnOK.Click += Btn_Click;
            btnCancel.Click += Btn_Click;
            btnDefault.Click += Btn_Click;

            txtWidth.Text = DefaultSize.Width.ToString();
            txtHeight.Text = DefaultSize.Height.ToString();

            SelInch.CheckedChanged += CheckedChanged;
            SelCM.CheckedChanged += CheckedChanged;
            SelPixel.CheckedChanged += CheckedChanged;
        }

        private void CheckedChanged(object sender, EventArgs e)
        {
            var radioButton = sender as RadioButton;

            var width = myFileInfo.picBoxSize.Width;
            var height = myFileInfo.picBoxSize.Height;

            if(radioButton.Checked)
            {
                switch (radioButton.Name)
                {
                    case "SelInch":
                        _currentSize = new SizeF(width * 0.013889f * 2.54f, height * 0.013889f * 2.54f);
                        myFileInfo.unit = Unit.Inch;
                        break;
                    case "SelCM":
                        _currentSize = new SizeF(width * 0.003333f * 2.54f, height * 0.003333f * 2.54f);
                        myFileInfo.unit = Unit.CM;
                        break;
                    case "SelPixel":
                        _currentSize = new SizeF(width, height);
                        myFileInfo.unit = Unit.Pixel;
                        break;
                    default:
                        _currentSize = new SizeF(width, height);
                        myFileInfo.unit = Unit.Pixel;
                        break;
                }
                this.txtWidth.Text = Math.Round(_currentSize.Width, 0).ToString();
                this.txtHeight.Text = Math.Round(_currentSize.Height, 0).ToString();
            }
        }

        public FrmImageProperty(PPFileInfo myFile) : this()
        {
            if (myFile == null) return;

            this.myFileInfo = myFile;

            if (myFile.fileInfo == null) return;

            this.txtFileSpace.Text = $"{Math.Round(myFile.fileInfo.Length / 1024f, 2)}k";
            this.txtFileLastSaveTime.Text = myFile.fileInfo.LastWriteTime.ToString();

            switch (myFile.unit)
            {
                case Unit.Inch:
                    SelInch.Checked = true;
                    _currentSize = new SizeF(myFile.picBoxSize.Width * 0.013889f * 2.54f, myFile.picBoxSize.Height * 0.013889f * 2.54f);
                    break;
                case Unit.CM:
                    SelCM.Checked = true;
                    _currentSize = new SizeF(myFile.picBoxSize.Width * 0.003333f * 2.54f, myFile.picBoxSize.Height * 0.003333f * 2.54f);
                    break;
                case Unit.Pixel:
                default:
                    SelPixel.Checked = true;
                    _currentSize = new SizeF(myFile.picBoxSize.Width, myFile.picBoxSize.Height);
                    break;
            }

            this.txtWidth.Text = Math.Round(_currentSize.Width, 0).ToString();
            this.txtHeight.Text = Math.Round(_currentSize.Height, 0).ToString();
            this.txtFileDPI.Text = $"96 DPI";
        }

        /// <summary>
        /// 按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Click(object sender, EventArgs e)
        {
            var btn = sender as Button;
            switch (btn.Name)
            {
                case "btnOK":

                    int width = (int)Convert.ToSingle(txtWidth.Text), height = (int)Convert.ToSingle(txtHeight.Text);
                    this.myFileInfo.picBoxSize = new Size(width, height);

                    this.Close();

                    break;           
                case "btnCancel":
                    this.Close();
                    break;
                case "btnDefault":
                    txtWidth.Text = DefaultSize.Width.ToString();
                    txtHeight.Text = DefaultSize.Height.ToString();
                    break;
                default:
                    break;
            }

        }
    }
}
